#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <netinet/in.h>
#include <pthread.h>
#include <arpa/inet.h>



using namespace std;
#define BUFFERSIZE 1024

/* 获取CPU */
int main()
{
    ifstream ifs;

    ifs.open("/proc/meminfo");

    if(!ifs.is_open())
    {
        /* 打开文件失败 */
        exit(-1);
    }
    int totalMem;
    int freeMem;
    char buffer[BUFFERSIZE];
    while(!ifs.eof())
    {
        ifs.getline(buffer, sizeof(buffer));
        if((strstr(buffer, "MemTotal") != NULL) || (strstr(buffer, "MemAvailable") != NULL))
        {
            string lineData(buffer);
            //cout << lineData << endl;

            int startPos =0;
            while(!isdigit(lineData[startPos]))
            {
                startPos++;
            }
            int endPos = lineData.find(" kB", startPos);

            cout << lineData.substr(startPos, endPos - startPos) << endl;            
        }
        
        
    }


    int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in remoteAddress;
    memset(&remoteAddress, 0, sizeof(remoteAddress));
    remoteAddress.sin_family = AF_INET;
    remoteAddress.sin_port = 8080;       //端口
    const char * piblicIP = "120.26.3.91";
    inet_pton(AF_INET, piblicIP, (void*)&remoteAddress.sin_addr.s_addr);
    connect(sockfd, (struct sockaddr*)&remoteAddress, sizeof(remoteAddress));

    /* 数据上报 */
    float usage = 10.02;
    char data[64] = {0};
    while (1)
    {
        #if 0
        /* 格式化字符串 */
        snprintf(data, sizeof(data), "massage:%.2f%%", usage);
        write(sockfd, &data, sizeof(data));
        memset(data, 0, sizeof(data));
        sleep(3);
        #endif


        
    }
    

    return 0;
}